home *** CD-ROM | disk | FTP | other *** search
- #include <fcntl.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <stdio.h>
- #include <io.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
-
-
- #define version "1.0"
-
- #define TRUE 1
- #define FALSE 0
-
- #define INPUT_COUNT 16
- #define OUTPUT_COUNT INPUT_COUNT*5 + 2
-
-
-
-
- main(argc, argv)
- int argc;
- char *argv[];
-
- {
- int input_fnum, output_fnum;
- int i, cnt;
- unsigned char buffer[INPUT_COUNT];
- unsigned char outbuf[OUTPUT_COUNT+1];
-
- if (argc < 3)
- {
- cputs("Program HEXFILE; version ");
- cputs(version);
- cputs("\n\rProgram usage: hexfile <input file> <output file>\n");
- return;
- }
- else
- {
- if (!strcmp(argv[1], argv[2]))
- {
- cputs("Error -- output file must different than input file");
- return;
- }
-
- if ((input_fnum = open(argv[1], O_RDONLY | O_BINARY)) == -1)
- {
- cputs("Error -- Unable to open input file");
- return;
- }
-
- /* see if the output file exists */
- if ((output_fnum =
- open(argv[2], O_CREAT | O_EXCL, S_IWRITE)) == -1)
- {
- if (errno == EEXIST)
- {
- cputs("The output file already exists. Write over it? ");
- i = getche();
- if (i != 'y' && i != 'Y') exit(1);
- }
- else
- {
- cputs("Error -- Unable to create the output file.\n\r");
- exit(1);
- }
- }
- remove(argv[2]);
- output_fnum =
- open(argv[2], O_CREAT | O_BINARY | O_RDWR, S_IWRITE);
- if (output_fnum == -1)
- {
- cputs("Error -- Unable to create the output file.\n\r");
- return;
- }
-
- }
-
-
-
-
- while ((cnt = read(input_fnum, buffer, INPUT_COUNT)) > 0)
- {
- hexize(buffer, outbuf, &cnt);
- write(output_fnum, outbuf, cnt);
- }
-
- close(input_fnum);
- close(output_fnum);
- exit(1);
- }
-
-
-
- hexize(char *in, char *out, int *cnt)
- {
- int i, j;
- unsigned char *p, *q;
-
- p = in;
- q = out;
- for (i = *cnt; i > 0; i--, p++)
- {
- q[0] = '0';
- q[1] = 'x';
- q += 2;
- itoh(*p, q); /* this procedure always returns 2 chars + '\0' */
- q[2] = ',';
- q += 3;
- }
- q[0] = '\r';
- q[1] = '\n';
- *cnt = *cnt * 5 + 2;
- }
-
-
-
-
- itoh(n, s) /* convert the byte 'n' to the string 's' */
- unsigned char s[];
- unsigned char n;
- {
- int i, j, hold;
-
-
- i = 0;
-
- do { /* generate digits in reverse order */
- j = n % 16;
- if (j < 10)
- s[i++] = j + '0';
- else
- s[i++] = j - 10 + 'A';
- }
- while ((n /= 16) > 0); /* delete it */
-
- s[i] = '\0';
- if (strlen(s) == 1)
- {
- s[1] = s[0];
- s[0] = '0';
- s[2] = '\0';
- }
- else
- {
- hold = s[0];
- s[0] = s[1];
- s[1] = hold;
- }
- }
-
-
-
-